package org.deri.socializer.activities.start;


import org.deri.socializer.SocializerApplication;
import org.deri.socializer.activities.chat.ChatListActivity;
import org.deri.socializer.activities.config.ConfigActivity;
import org.deri.socializer.activities.map.GoogleMapsActivity;
import org.deri.vor.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;

public class StartActivity extends Activity {

	
	SocializerApplication app;
	GridView gridView							= null;

	boolean created = false;
	
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState)  {
    	super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.socializer_main);
 
       
        
        Button button = (Button)findViewById(R.id.buttonStartMapView);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) { startMapActivity(); }
        });
        
        button = (Button)findViewById(R.id.buttonStartChats);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) { startChatListActivity(); }
        });

        button = (Button)findViewById(R.id.buttonStartConfig);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) { startConfigActivity(); }
        });

        share_button();
       
        
        
        this.app = ((SocializerApplication)getApplicationContext());
   
        if (!this.app.hasXmppConnection)
        	this.showXmppAlertDialog();
        
        this.created = true;
    }


    
    public void showXmppAlertDialog() {
    	final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    	alertDialog.setTitle("XMPP connection failed!");
    	alertDialog.setMessage("Please check your XMPP connection settings");
    	alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
    	   public void onClick(DialogInterface dialog, int which) {
    	      // here you can add functions
    		  alertDialog.hide();
    	   }
    	});
    	alertDialog.show();
    }
    
    
    public void switchToActivity(int gridViewPos) {
    	long id = this.gridView.getAdapter().getItemId(gridViewPos);
    	if (id == R.drawable.google_maps_icon2) 
    		startMapActivity();
    	else if (id == R.drawable.chat2) 
    		startChatListActivity();
    	else if (id == R.drawable.settings2)
    		startConfigActivity();
    	else 
    		return;
    }
	
    
    private void startMapActivity() {
    	Intent intent = new Intent(getApplicationContext(), GoogleMapsActivity.class);
    	startActivity(intent);
    	finish();
    }

    private void startChatListActivity() {
    	Intent intent = new Intent(getApplicationContext(), ChatListActivity.class);
    	startActivity(intent);
    	finish();
    }
    
    private void startConfigActivity() {
    	Intent intent = new Intent(getApplicationContext(), ConfigActivity.class);
    	startActivity(intent);
    	finish();
    }
    

    
    @Override
	protected void onPause() {
	    super.onPause();
	    created = false;
	    SocializerApplication.runningActivities--;
	}
    
	@Override
	protected void onResume() {
		super.onResume();
		if (!created) {
	    	Intent intent = getIntent();
	    	finish();
	    	startActivity(intent);
	    }
		SocializerApplication.runningActivities++;
	}
	
	@Override
	protected void onDestroy() {
	    super.onDestroy();
	    this.app.checkActivityState();
	}

	
	private void share_button(){
		Button button = (Button) findViewById(R.id.btnBack);
		button.setOnClickListener(ms_layout);

		// About Button OnClick
		button = (Button) findViewById(R.id.btnAbout);
		button.setOnClickListener(ms_layout);

		// Home Button OnClick
		button = (Button) findViewById(R.id.btnHome);
		button.setOnClickListener(ms_layout);
		
		ImageView im  = (ImageView) findViewById(R.id.vor_icon);
		im.setClickable(true);
		im.setOnClickListener(ms_layout);

		im = (ImageView) findViewById(R.id.nuig_icon);
		im.setClickable(true);
		im.setOnClickListener(ms_layout);

		im = (ImageView) findViewById(R.id.deri_icon);
		im.setClickable(true);
		im.setOnClickListener(ms_layout);
	}
    
    
    private OnClickListener ms_layout	=	new OnClickListener(){

		@Override
		public void onClick(View v) {
			switch (v.getId()){
			case R.id.btnAbout  : { jump(72); break;}
			case R.id.btnHome 	: { jump(2); break;}
			case R.id.btnBack  	: { jump(2); break;}
			case R.id.vor_icon  : { jump(3); break;}
			case R.id.deri_icon : { jump(19); break;}
			case R.id.nuig_icon : { jump(40); break;}
			}
		}
    };
    
    
    private void jump(int page){
    	Intent i = new Intent();
    	i.putExtra("cl",page);
    	i.setClassName("org.deri.vor", "org.deri.vor.VovolOceanRaceActivity");
		startActivity(i);
		finish();
    }
	

}






final class MainViewClickListener implements OnItemClickListener {

	StartActivity startActivity;
	
	public MainViewClickListener(StartActivity startActivity) {
		this.startActivity = startActivity;
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		this.startActivity.switchToActivity(position);
    }
	
}